<template>
  <div class="chart-select" :style="position">
    <ul class="list">
      <div class="show-box">
        <div v-if="selected.length === SelectList.length">全部</div>
        <div v-else>
          <div v-if="selected.length === 1">{{ selected[0].label }}</div>
          <div v-else>----</div>
        </div>
      </div>
      <div v-show="listShow" class="select-list">
        <li
          class="item"
          :type="item.type"
          v-for="(item, index) in SelectList"
          :key="index"
          :style="{
            'font-size': isMobile ? 8 + 'px' : 12 + 'px',
            cursor: index !== 0 ? 'pointer' : ''
          }"
        >
          <label :for="item.typeKey + 'b'">
            {{ item.label }}&nbsp;&nbsp;
            <input
              :id="item.typeKey + 'b'"
              type="checkbox"
              v-model="selected"
              @click="isSelectAll($event, item)"
              :value="item"
            />
          </label>
        </li>
        <div class="footer">
          <div class="item">
            <div class="btn save" @click="save">确认</div>
          </div>
          <div class="item">
            <div class="btn reset" @click="reset">重置</div>
          </div>
        </div>
      </div>
    </ul>
    <div class="static-select-icon" @click="showList">
      <i class="el-icon-arrow-up" v-if="listShow"></i>
      <i v-else class="el-icon-arrow-down"></i>
    </div>
  </div>
</template>

<script>
import { isMobile } from '../utils/index'
export default {
  name: 'ChartSelectBar',
  props: {
    title: {
      type: String,
      default: () => {
        return ''
      }
    },
    legend: {
      type: Array,
      default: () => {
        return []
      }
    },
    SelectList: {
      type: Array,
      default: () => {
        return []
      }
    },
    position: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      listShow: false,
      selected: [],
      showLabelStatus: false,
      labelList: [],
      isMobile: false
    }
  },
  mounted () {
    this.$EventBus.$on('asyncSelect', (item, legend) => {
      const select = JSON.parse(JSON.stringify(this.selected))
      const all = legend.filter((item) => { return item.active })
      if (all.length === 10) {
        this.selected = this.SelectList
        return
      }

      if (item.key <= 3) {
        if (item.active) {
          if (legend.filter((item) => { return item.key <= 3 && item.active }).length === 4) {
            if (select.filter((item) => { return item.typeKey === 1 }).length === 0) {
              select.push({ 'label': '月度', 'type': 'block', 'color': '#a0cbe8', 'which': 'bar', 'typeKey': 1 })
            } else {
              const i = select.findIndex((item) => { return item.typeKey === 1 })
              if (i > -1) {
                select.splice(i, 1)
              }
            }
          }
        } else {
          const i = select.findIndex((item) => { return item.typeKey === 1 })
          if (i > -1) {
            select.splice(i, 1)
          }
        }
        this.selected = select
      }
      if (item.key > 3 && item.key <= 7) {
        if (item.active) {
          if (legend.filter((item) => { return item.key > 3 && item.active && item.key <= 7 }).length === 4) {
            if (select.filter((item) => { return item.typeKey === 2 }).length === 0) {
              select.push({ 'label': '累计', 'type': 'block', 'color': '#008ad6', 'which': 'bar', 'typeKey': 2 })
            } else {
              const i = select.findIndex((item) => { return item.typeKey === 2 })
              if (i > -1) {
                select.splice(i, 1)
              }
            }
          }
        } else {
          const i = select.findIndex((item) => { return item.typeKey === 2 })
          if (i > -1) {
            select.splice(i, 1)
          }
        }
        this.selected = select
      }
      if (item.key > 7) {
        if (item.active) {
          if (legend.filter((item) => { return item.key > 7 && item.active }).length === 2) {
            if (select.filter((item) => { return item.typeKey === 3 }).length === 0) {
              select.push({ 'label': '现金流', 'type': 'block', 'color': '#abe7be', 'which': 'bar', 'typeKey': 3 })
            } else {
              const i = select.findIndex((item) => { return item.typeKey === 3 })
              if (i > -1) {
                select.splice(i, 1)
              }
            }
          }
        } else {
          const i = select.findIndex((item) => { return item.typeKey === 3 })
          if (i > -1) {
            select.splice(i, 1)
          }
        }
        this.selected = select
      }

      if (legend.filter((item) => { return item.key <= 3 && !item.active }).length && legend.filter((item) => { return item.key > 3 && !item.active && item.key <= 7 }).length && legend.filter((item) => { return !item.active && item.key > 7 }).length) {
        this.selected = []
      }
    })
    this.isMobile = isMobile()
    setTimeout(() => {
      this.selected = this.SelectList
    }, 500)
  },
  watch: {
    selected: function (val) {
      console.log(val)
    }
  },
  methods: {
    save () {
      console.log(this.selected)
      this.$emit('selectSucessBar', this.selected)
      this.listShow = false
    },
    reset () {
      this.selected = this.SelectList
    },
    isSelectAll (event, item) {
      const isChecked = event.target.checked
      if (isChecked && item.typeKey === 0) {
        this.selected = this.SelectList
      } else if (!isChecked && item.typeKey === 0) {
        this.selected = []
      }
      if (this.selected.length === 2 && isChecked) {
        this.selected = this.SelectList
        return
      }
      if (item.typeKey !== 0) {
        const arr = []
        for (let index = 0; index < this.selected.length; index++) {
          const item = this.selected[index]
          if (item.typeKey !== 0) {
            arr.push(item)
          }
        }
        this.selected = arr
      }
    },
    showBar (list) {
      if (!list.length) {
        this.selected = []
      } else {
        const Arr = []
        for (let i = 0; i < list.length; i++) {
          const listItemKey = list[i]
          for (let j = 0; j < this.SelectList.length; j++) {
            const originItem = this.SelectList[j]
            if (listItemKey === originItem.typeKey) {
              Arr.push(originItem)
              break
            }
          }
        }
        this.selected = Arr
      }
      this.save()
    },
    showList () {
      if (this.listShow) {
        this.save()
      } else {
        this.listShow = !this.listShow
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.show-box {
  text-align: center;
  height: 24px;
  line-height: 24px;
  width: 120px;
  border: 1px solid #0e6aa9;
  div {
    color: #fff;
  }
}

input {
  cursor: pointer !important;
  position: absolute;
  right: 2px;
  top: 4px;
  margin-left: 10px !important;
  display: inline-block;
  width: 10px;
  height: 10px;
}

.chart-select {
  position: absolute;
  z-index: 99;
  .item-static-status {
    display: flex;
    align-items: center;
    line-height: 24px;
    color: #c9cacc;
  }
  .list {
    float: left;
  }
  .item {
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    line-height: 26px;
    padding: 0 10px !important;
    font-size: 12px;
    background-color: #233a61;
    label {
       cursor: pointer;
 color: #fff;
    }
    &:hover {
      background-color: #556b91;
    }
  }
  .block {
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-right: 5px;
  }
  .solid {
    display: inline-block;
    width: 8px;
    height: 2.4px;
    margin-right: 5px;
  }
  .dashed {
    display: inline-block;
    width: 3px;
    border-width: 1.2px;
    border-style: dashed;
    &:first-child {
      margin-right: 2px;
    }
    &:last-child {
      margin-right: 5px;
    }
  }
  .static-select-icon {
    position: absolute;
    right: 0;
    float: left;
    width: 24px;
    height: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    .arrow {
      width: 20px;
      height: 20px;
    }
    i {
      color: #fff;
    }
  }
}
.rote {
  transform: rotate(180deg);
}
.footer {
  height: 30px;
  display: flex;
  .item {
    flex: 1;
    .btn {
      cursor: pointer;
      text-align: center;
      margin: 0 auto;
      width: 40px;
      height: 20px;
      line-height: 20px;
      border-radius: 4px;
    }
    .save {
      color: rgb(255, 255, 255);
      background-color: rgb(103, 194, 58);
      border-color: rgb(103, 194, 58);
    }
    .reset {
      color: rgb(255, 255, 255);
      background-color: rgb(230, 162, 60);
      border-color: rgb(230, 162, 60);
    }
  }
}
.select-list {
  z-index: 10000;
  width: 120px
}
</style>
